import 'package:flutter_magic_planet/model/comic.dart';
import 'package:flutter_magic_planet/model/comic_chapter.dart';
import 'package:flutter_magic_planet/model/comic_content.dart';

import 'request.dart';

class APIComicPingcc {
  static const ID = 'api-pingcc-cn-comic';

  /// https://easydoc.net/doc/45910076/WP3yrgI7/JS4OL8uC
  static const URL_PING_CC = 'http://api.pingcc.cn';

  /// 漫画搜索 http://api.pingcc.cn/cartoon/search/title/一人之下/1/10
  static const String COMIC_SEARCH = '/cartoon/search/';

  /// 漫画章节 http://api.pingcc.cn/cartoonChapter/search/1yirenzhixia
  static const String COMIC_CHAPTER_SEARCH = '/cartoonChapter/search/';

  /// 漫画内容 http://api.pingcc.cn/cartoonContent/search/11595866
  static const String COMIC_CONTENT_SEARCH = '/cartoonContent/search/';

  /// 漫画搜索
  static Future<List<Comic>> getComics(
      {required String option, required String key}) async {
    final Map response = await Request.get(
        action: '${URL_PING_CC + COMIC_SEARCH}$option/$key/1/99');
    if (response['code'] == 0) {
      List data = response['data']['data'];
      return data.map((e) => Comic.fromJson(e)).toList();
    }
    return [];
  }

  /// 根据ID获取漫画章节
  static Future<List<ComicChapter>> getComicChapters(
      {required String id}) async {
    final Map response =
        await Request.get(action: '${URL_PING_CC + COMIC_CHAPTER_SEARCH}$id');
    if (response['code'] == 0) {
      List data = response['data']['data'];
      return data.map((e) => ComicChapter.fromJson(e)).toList();
    }
    return [];
  }

  /// 根据ID获取漫画内容
  static Future<ComicContent?> getComicContent({required String id}) async {
    final Map response =
        await Request.get(action: URL_PING_CC + COMIC_CONTENT_SEARCH + id);
    if (response['code'] == 0) {
      var data = response['data']['data'];
      return ComicContent.fromJson(data);
    }
    return null;
  }
}
